var DynamicChart = (function(w) {
	var DynamicChart = function(chartDivId) {
		return new DynamicChart.fn.init(chartDivId)
	}

	DynamicChart.fn = DynamicChart.prototype = {
		constructor : DynamicChart,
		init : function(chartDivId) {
			this.chartDom = document.getElementById(chartDivId)
			this.chart = echarts.init(this.chartDom)
			this.xAxisData = []
			this.series = []
			this.chartOption = {
				title : {text : '',textStyle : {color : 'rgba(255,255,255,.6)'},x : '20px'},
				tooltip : {trigger : 'axis',axisPointer : {type : 'shadow'}},
				grid : {left : '1%',top : '40px',right : '1%',bottom : '10',containLabel : true},
				xAxis : {type : 'category',axisLabel : {interval : 60,show : true,textStyle : {color : 'rgba(255,255,255,.6)',fontSize : '12'},rotate : 40},data : this.xAxisData},
				yAxis : {
					type : 'value',
					// min : -500,
					// max : 30000,
					axisLabel : {
						// formatter: '{value} %'
						show : true,textStyle : {color : 'rgba(255,255,255,.6)',fontSize : '12'}
					},
					splitLine : {lineStyle : {color : 'rgba(255,255,255,.1)'}}
				},
				series : this.series
			};
			this.chart.setOption(this.chartOption);
		},
		initDynamicChart : function(title, elementCode, elementNum) {
			for (var i = 0; i < 1000; i++) {
				this.xAxisData.push('')
			}
			for (i = 0; i < elementNum; i++) {
				var ev = elementCode + (i + 1)
				var seriesData = {
					name : ev,
					data : [],
					type : 'line',
					showSymbol : false,
					itemStyle : {
						normal : {
							opacity : 1,
							barBorderRadius : 5
						}
					}
				}
				this.series.push(seriesData)
			}
			for (var i = 0; i < elementNum; i++) {
				for (var j = 0; j < 1000; j++) {
					this.series[i].data.push(0)
				}
			}
			this.chart.setOption({
				title : {
					text : title,
				},
				xAxis : {
					data : this.xAxisData
				},
				series : this.series
			})
		},
		setDynamicDatas : function(realtimeJsonData) {
			this.xAxisData.shift()
			
			let time = realtimeJsonData.message.time.substring(11)
			this.xAxisData.push(time)

			var newElementDataList = []
			for (var i = 0; i < realtimeJsonData.message.data.length; i++) {
				var data = realtimeJsonData.message.data[i]
				if (data.code == waMap.index_chart_element) {
					newElementDataList.push(data)
				}
			}
			for (var j = 0; j < newElementDataList.length; j++) {
				var data = newElementDataList[j]
				this.series[j].data.push(data.value)
				this.series[j].data.shift()
			}
			this.chart.setOption({
				xAxis : {
					data : this.xAxisData
				},
				series : this.series
			})
		},
		setDynamicData : function(xAxisData, seriesData) {
			this.xAxisData.push(xAxisData)
			this.series[0].data.push(seriesData)
			this.xAxisData.shift()
			this.series[0].data.shift()
			this.chart.setOption({
				xAxis : {
					type : 'category',
					boundaryGap : false,
					rotate:40,
					data : this.xAxisData
				},
				series : this.series
			})
		},
		setClick : function(config) {
			this.chart.getZr().on('click', function(param) {
				window.open(config.path);
			});
		}
	}

	DynamicChart.fn.init.prototype = DynamicChart.fn
	return DynamicChart
})()